import pandas as pd
from openpyxl import load_workbook


def dfgx_df更新(index: int, df: pd.DataFrame, ser: pd.Series):
    for key in ser.keys():
        df.loc[index, key] = ser[key]


def get_series_横向合并(list_ser: list[pd.Series]):
    ser_out = pd.Series()
    for ser in list_ser:
        ser_out.update(ser)
        for col in ser.index:
            if col not in ser_out.index:
                ser_out[col] = ser[col]
    return ser_out


def get_df_series_纵向合并为df(list_ser: list[pd.Series]):
    if len(list_ser) == 0:
        return None
    df = pd.DataFrame(columns=list_ser[0].index)
    list_out = []
    list_out.append(df)
    for ser in list_ser:
        list_out.append(ser.to_frame().T)
    # list_ser.insert(0, df)
    df = pd.concat(list_out, axis=0, ignore_index=True)
    return df


def get_df_from_excel_从excel文件中获取df数据(filename, gzb_工作表=None):
    # 读取 Excel 文件
    wb = load_workbook(filename)
    if gzb_工作表:
        # 选择第一个工作表
        ws = wb[gzb_工作表]
    else:
        ws = wb.active
    # 从工作表中读取数据并转换为 DataFrame
    data = ws.values
    cols = next(data)
    data = list(data)
    df = pd.DataFrame(data, columns=cols)
    return df
